<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class AlumiController extends AdminController{
    
    public function actionIndex(){
        $Alumi = new Alumni();
        $Alumi->unsetAttributes();
        
        
        if(isset($_GET['Alumni'])){          
            $Alumi->attributes = $_GET['Alumni'];
        }
        
        
         $this->render('index',array(
          'Alumi'=>$Alumi  
        ));
  
         
    }
    
      public function actionAdd(){
        
       $Alumni = new Alumni();
       $Alumni->unsetAttributes();
       
       $AlumiUser = new AlumniUser('add');
       $AlumiUser->unsetAttributes();
       
        if((isset($_POST['ajax']) && $_POST['ajax']==='Alumni-form'))
        {
              //两个model ajax验证       
            //p_e($_POST);           
            echo CActiveForm::validate(array($Alumni,$AlumiUser));  
            Yii::app()->end();
  
        }
        
       if(isset($_POST['Alumni']) && !isset($_POST['ajax'])){
           
           $Alumni->attributes = $_POST['Alumni']; 
           
           if(!isset($_POST['Alumni']['school_id'])){//二级
                $Alumni->school_id =0;
           }
           
           if(!isset($_POST['Alumni']['city_id'])){//三级
                $Alumni->city_id =  Yii::app()->user->city_id;
           }
           
           
           $Alumni->ctime =time();
           
           if(Yii::app()->user->parent_id)
                $Alumni->parent_id=0;
            else 
                $Alumni->parent_id=Yii::app()->user->Alumni_id;
    
           $Alumni->status=1;
           $Alumni->is_del=2;
           

           $transaction  =Yii::app()->db->beginTransaction();
           if($Alumni->save()){
                    $AlumniID = Yii::app()->db->getLastInsertID();
                    $AlumiUser->alumni_id = $AlumniID;
                    $AlumiUser->ctime =time();
                    $AlumiUser->status=1;
                    $AlumiUser->is_del=2;
                    $AlumiUser->attributes = $_POST['AlumniUser']; 
                    $AlumiUser->password=md5($AlumiUser->password);
                   //p_e($AlumiUser);
                    if($AlumiUser->save(FALSE)){
                       $transaction->commit();
                       Yii::app()->user->setFlash('addform', "添加成功！");
                       Yii::app()->jump->success("添加成功",yii::app()->createUrl('xyhadm/alumi/index'));
                      //$this->refresh(); 
                    }else{
                        $transaction->rollback();
                       $ErrorMessage = BootExtend::GetErrorMassage($AlumiUser);
                        Yii::app()->jump->error('账号添加添加失败'.$ErrorMessage.'');
                       // $this->refresh();
                    }      
           }else{
                $transaction->rollback();
                $ErrorMessage = BootExtend::GetErrorMassage($Alumni);
                Yii::app()->jump->error('院校添加失败'.$ErrorMessage.'');
                $this->refresh();
           } 
       }
        $this->render('add',array(
         'Alumni' =>$Alumni,
         'AlumiUser'=>$AlumiUser
        ));

    }
    
    
    
     public function actionEdit($id){
        
           $Alumni = Alumni::model()->findByPk($id);  
           
           $AlumiUser =  AlumniUser::model()->findByAttributes(array('alumni_id'=>$id));
           
           $AlumiUser->password_repeat =$AlumiUser->password;
           
       if(isset($_POST['Alumni'])){

           $Alumni->attributes = $_POST['Alumni']; 
           
//           if(!isset($_POST['Alumni']['school_id'])){//二级
//                $Alumni->school_id =0;
//           }
//           
//           if(!isset($_POST['Alumni']['city_id'])){//三级
//                $Alumni->city_id =  Yii::app()->user->city_id;
//           }
           
           
           $Alumni->mtime =time();
           
//           if(Yii::app()->user->parent_id)
//                $Alumni->parent_id=0;
//            else 
//                $Alumni->parent_id=Yii::app()->user->Alumni_id;
    
           $Alumni->status=1;
           $Alumni->is_del=2;
           
           
           $transaction  =Yii::app()->db->beginTransaction();
           if($Alumni->save()){
                    $AlumiUser->mtime =time();
                    $AlumiUser->status=1;
                    $AlumiUser->is_del=2;
                    
                    if($_POST['AlumniUser']['password'] == $AlumiUser->password){
                         unset($_POST['AlumniUser']['password']);
                          $AlumiUser->attributes = $_POST['AlumniUser']; 
                    }else{
                         $AlumiUser->attributes = $_POST['AlumniUser']; 
                         $AlumiUser->password=md5($AlumiUser->password);
                    }                  
                   
                   //p_e($AlumiUser);
                    if($AlumiUser->save(FALSE)){
                       $transaction->commit();
                       Yii::app()->user->setFlash('addform', "修改成功！");
                       Yii::app()->jump->success("修改成功",yii::app()->createUrl('xyhadm/alumi/index'));
                      //$this->refresh(); 
                    }else{
                       $transaction->rollback();
                       $ErrorMessage = BootExtend::GetErrorMassage($AlumiUser);
                        Yii::app()->jump->error('账号修改失败'.$ErrorMessage.'');
                       // $this->refresh();
                    }      
           }else{
                $transaction->rollback();
                $ErrorMessage = BootExtend::GetErrorMassage($Alumni);
                Yii::app()->jump->error('院校修改失败'.$ErrorMessage.'');
                $this->refresh();
           } 
       }
       
       
        $this->render('edit',array(
         'Alumni' =>$Alumni,
         'AlumiUser'=>$AlumiUser
        ));

    }
    
    
    
    
    
}